package list;

public class ReverseList {
    public static void main(String[] args) {
        Node head = new Node((int)(Math.random()*10)+1);
        head.next = new Node((int)(Math.random()*10)+1);
        head.next.next = new Node((int)(Math.random()*10)+1);
        head.next.next.next = new Node((int)(Math.random()*10)+1);
        head.next.next.next.next = new Node((int)(Math.random()*10)+1);
        Node n = head;
        while (n != null) {
            System.out.print(n.value+"\t");
            n = n.next;
        }
        System.out.println();
        Node m = reverseList(head);  //----------------
        while (m != null) {
            System.out.print(m.value+"\t");
            m = m.next;
        }
    }

//    public static Node reverseList(Node head) {
//        Node a = head;
//        Node b;
//        Node c = null;
//        while (a != null) {
//            b = a.next;   //--------------
//            a.next = c;  //-------------
//            c = a;
//            a = b;
//        }
//        return c;
//    }

    public static Node reverseList(Node head) {
        Node pri = head;
        Node newHead = null;
        Node temp;
        while (pri != null) {
            temp = pri.next;
            pri.next = newHead;
            newHead = pri;
            pri = temp;
        }
        return newHead;
    }

    static class Node {
        private int value;
        private Node prev;
        private Node next;

        public Node(int value) {
            this.value = value;
        }
    }
}
